package com.zhiyou100.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.RegistrationInfor;
import com.zhiyou100.service.RegistrationService;

@Controller
public class RegistrationController {
	@Autowired
	private RegistrationService regService;
	// 查询
	@RequestMapping("/registration/list")
	public String registrationList(@RequestParam(defaultValue="1")int pageNo,
			@RequestParam(defaultValue="")String medical_records,
			@RequestParam(defaultValue="")String doctor_name,
			@RequestParam(defaultValue="")String secco_name,Model model) {
		// 模糊查询
		Map<String,Object> map = new HashMap<>();
		map.put("medical_records", medical_records);
		map.put("doctor_name", doctor_name);
		map.put("secco_name", secco_name);
		
		// 分页
		PageHelper.startPage(pageNo, 3);
		// 调用服务层
		List<RegistrationInfor> lists = regService.findAll(map);
		System.out.println("查询list "+lists);
		PageInfo<RegistrationInfor> pageInfo = new PageInfo<>(lists);
		model.addAttribute("lists", pageInfo.getList());
		model.addAttribute("map", map);
		model.addAttribute("page", pageInfo);
		return "forward:/view/registration/list.jsp";
	}
	// 详情
	@RequestMapping("/registration/detail")
	public String registrationDetail(String medical_record,Model model) {
		RegistrationInfor ri = regService.findById(medical_record);
		System.out.println("ri: "+ri);
		System.out.println("证件类型: "+ri.getCertificateType().getCf_name());
		System.out.println("科室名称: "+ri.getSection().getSecco_name());
		System.out.println("医生姓名: "+ri.getDoctor().getDoctor_name());
		model.addAttribute("ri", ri);
		return "forward:/view/registration/detail.jsp";
	}
	
	// 退号
	@RequestMapping("/registration/delete")
	public String registrationDelete(String medical_record) {
		regService.deleteById(medical_record);
		return "forward:/registration/list";
	}
	// 更新 查询
	@RequestMapping(value="/registration/edit")
	public String registrationEdit(String medical_record,Model model) {
		RegistrationInfor ri = regService.editById(medical_record);
		System.out.println("更新查询到的ri"+ri);
		model.addAttribute("ri", ri);
		return "forward:/view/registration/edit.jsp";	
	}
	
	// 更新 插入
	@RequestMapping("registration/updata")
	public String registrationUpdata(RegistrationInfor ri) {
		System.out.println("更新registration:"+ri);
		// 调用服务层
		regService.updata(ri);
		return "forward:/registration/list";
		
	}
	
	// 添加
	@RequestMapping(value="registration/add",method=RequestMethod.GET)
	public String registrationAdd() {
		return "forward:/view/registration/add.jsp";
	}
	@RequestMapping(value="registration/add",method=RequestMethod.POST)
	public String registrationAdd2(RegistrationInfor ri) {
		// 调用服务层
		regService.add(ri);
		return "forward:/registration/list";
	}
}
